package cn.yunhe.controller;

import cn.yunhe.entity.*;
import cn.yunhe.services.IXsTreamServices;
import cn.yunhe.services.impl.*;
import cn.yunhe.util.StringToMap;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

import static cn.yunhe.util.ImgToString.imgToString;

/**
 * Created by Administrator on 2018/5/23.
 */
@RestController
@RequestMapping("/problem")
public class ProblemController {
    @Resource
    private ProblemServices problemServices;
    @Resource
    private LibServicesImpl libServices;
    @Resource
    private TeacherServicesImpl teacherServicesImpl;
    @Resource
    private TypeServicesImpl typeServicesImpl;
    @Resource
    private PointServicesImpl poinServicesImpl;
    @Resource
    private IXsTreamServices xsTreamServices;

    ModelAndView mv=new ModelAndView();
    LinkedHashMap<String,String> map=new LinkedHashMap<>();
    LinkedHashMap<String,String> mapimg=new LinkedHashMap<>();

    /**
     * 添加考题
     * @param problem
     * @param foreign
     * @return
     * @throws IOException
     */
    //单选
    @RequestMapping("/single")
    @ResponseBody
    public int updataProblem(Problem problem, Foreign foreign) throws IOException {
        imgToMap(problem);
        map.put("A",foreign.getPro_choiceA());
        map.put("B",foreign.getPro_choiceB());
        map.put("C",foreign.getPro_choiceC());
        map.put("D",foreign.getPro_choiceD());
        int resout=common(map,set(problem,foreign));
        return resout;
    }
    //判断
    @RequestMapping("/judge")
    @ResponseBody
    public int judgeProblem(Problem problem, Foreign foreign) throws IOException {
        imgToMap(problem);
        map.put("T","T");
        map.put("F","F");
        int resout=common(map,set(problem,foreign));
        return resout;
    }
    //多选题
    @RequestMapping("/multiSelect")
    @ResponseBody
    public int multiSelectProblem(Problem problem, Foreign foreign) throws IOException {
        if (foreign.getPro_choiceF() == null) {
            map.put("A",foreign.getPro_choiceA());
            map.put("B",foreign.getPro_choiceB());
            map.put("C",foreign.getPro_choiceC());
            map.put("D",foreign.getPro_choiceD());
            map.put("E",foreign.getPro_choiceE());
        } else {
            map.put("A",foreign.getPro_choiceA());
            map.put("B",foreign.getPro_choiceB());
            map.put("C",foreign.getPro_choiceC());
            map.put("D",foreign.getPro_choiceD());
            map.put("E",foreign.getPro_choiceE());
            map.put("F",foreign.getPro_choiceF());
        }
        int resout=common(map,set(problem,foreign));
        return resout;
    }
    //共有的类
    public int common(LinkedHashMap map,Problem problem){
        java.sql.Date data=new java.sql.Date(new Date().getTime());
        XsRequset xsRequset=new XsRequset();
        xsRequset.setChoiceImgList((LinkedHashMap<String, String>) problem.getPro_img());
        xsRequset.setChoiceList(map);
        problem.setPro_choice(xsTreamServices.objectToXml(xsRequset));
        problem.setDate(data);
        int resout=problemServices.insertProblemServices(problem);
        return resout;
    }
    public Problem set(Problem problem, Foreign foreign){
        Teacher teacher=new Teacher();
        teacher.setTeacher_id(2);
        problem.setTeacher(teacher);
        Point point=new Point();
        point.setPoint_id(foreign.getPoint_id());
        problem.setPoint(point);
        Type type=new Type();
        type.setType_id(foreign.getType_id());
        problem.setType(type);
        Lib lib=new Lib();
        lib.setLib_id(foreign.getLib_id());
        problem.setLib(lib);
        return problem;
    }
    //判断有没有上传图片
    public void  imgToMap(Problem problem) throws IOException {
        String []str={"A","B","C","D","E","F","G","H","J"};
        if(problem.getFile()!=null){
            List list=imgToString(problem);
            for (int i=0;i<list.size();i++){
                mapimg.put(str[i],String.valueOf(list.get(i)));
            }
            problem.setPro_img(mapimg);
        }else{
            mapimg.put("A","");
            problem.setPro_img(mapimg);
        }
    }
    /**
     *查看全部试题
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public Map queryProblemByPage(int pageNum, int pageSize ,Problem problem ){
        List<Point> listP=poinServicesImpl.getPointLib(problem.getLib().getLib_id());
        Page page=new Page();
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("problem" ,problem);
        page = problemServices.queryListLikeProblem(map);
        map.put("listP" ,listP);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
     }
    @RequestMapping("/toList")
    public ModelAndView toList()
    {
        List<Lib> listLib=libServices.findLib();
        mv.addObject("listLib" ,listLib);
        List<Type> listType=typeServicesImpl.findType();
        mv.addObject("listType" ,listType);
        mv.setViewName("/ftl/listProblem");
        return mv;
    }

    @RequestMapping("/getTypeProblemCount")
    public int getTypeProblemCount(int type_id) {
        return problemServices.getTypeProblemCount(type_id);
    }

    @RequestMapping("/getTypeAndPointCount")
    public List<Integer> getTypeAndPointCount(
            @RequestParam(value = "type[]") Integer type[],
            Integer point
    ) {
        System.out.println(point);
        List<Integer> list = new ArrayList<>();
        List<Integer> types = Arrays.asList(type);
        for (int i = 0; i < types.size(); i++) {
            Map map = new HashMap<>();
            map.put("type_id", types.get(i));
            System.out.println(types.get(i));
            map.put("point_id", point);
            int a = problemServices.getTypeAndPointCount(map);
            System.out.println(a);
            list.add(a);
        }

        return list;
    }
    /**
     * 根据id试题查看
     * @return
     */
    @RequestMapping("/findProblem")
    @ResponseBody
    public ModelAndView findProblem(int pro_id){
         lookProblem(pro_id);
         mv.setViewName("ftl/updateProblem");
        return mv;
    }
    /**
     * 查看
     * @param
     * @return
     */
    @RequestMapping("/searchProblem")
    @ResponseBody
    public ModelAndView searchProblem(int pro_id) {
        lookProblem(pro_id);
        mv.setViewName("ftl/searchProblem");
        return mv;
    }
    /*查看公用的方法*/
    public void lookProblem(int pro_id ){
        Problem problem= problemServices.findProblem(pro_id);
        problem.setPro_img(StringToMap.imgToMap(problem.getPro_choice()));
        problem.setPro_mapname(StringToMap.stringToMap(problem.getPro_choice()));
        List<Type> listType=typeServicesImpl.findType();
        mv.addObject("listType",listType );
        List<Point> listPoint =poinServicesImpl.findPoint();
        mv.addObject("listPoint",listPoint );
        List<Teacher> listTeacher=teacherServicesImpl.findTeacher();
        mv.addObject("listTeacher",listTeacher);
        List<Lib> listLib =libServices.findLib();
        mv.addObject("listLib" ,listLib);
        mv.addObject("problem",problem );
        mv.addObject("mapSize",problem.getPro_mapname().size());
    }
    /**
     * 修改试题
     * @return
     */
 /*   *//*单选题*//*

    @RequestMapping("/updateChoice")
    @ResponseBody
    public int updateChoice(Problem problem,String pro_choiceA,String pro_choiceB,String pro_choiceC,String pro_choiceD){
        LinkedHashMap<String,String> map=new LinkedHashMap<>();
        map.put("A",pro_choiceA);
        map.put("B",pro_choiceB);
        map.put("C",pro_choiceC);
        map.put("D",pro_choiceD);
        xsRequset.setChoiceList(map);
        problem.setPro_choice(xsTreamServices.objectToXml(xsRequset));
        java.sql.Date date=new java.sql.Date(new Date().getTime());
        problem.setDate(date);
        return problemServices.updateProblem(problem);
    }
   *//* 多选题*//*
   @RequestMapping("/updateChoices")
   @ResponseBody
   public int updateChoices(Problem problem,Foreign foreign){
       LinkedHashMap<String,String> map=new LinkedHashMap<>();
       if(pro_choiceE==null){
           map.put("A",pro_choiceA);
           map.put("B",pro_choiceB);
           map.put("C",pro_choiceC);
           map.put("D",pro_choiceD);
       }else if (pro_choiceF==null){
           map.put("A",pro_choiceA);
           map.put("B",pro_choiceB);
           map.put("C",pro_choiceC);
           map.put("D",pro_choiceD);
           map.put("E",pro_choiceE);
       }else {
           map.put("A", pro_choiceA);
           map.put("B", pro_choiceB);
           map.put("C", pro_choiceC);
           map.put("D", pro_choiceD);
           map.put("E", pro_choiceE);
           map.put("F", pro_choiceF);
       }
       xsRequset.setChoiceList(map);
       problem.setPro_choice(xsTreamServices.objectToXml(xsRequset));
       java.sql.Date date=new java.sql.Date(new Date().getTime());
       problem.setDate(date);
       return problemServices.updateProblem(problem);
   }
    *//*判断*//*
    @RequestMapping("/updateJudge")
    @ResponseBody
    public int updateChoice(Problem problem,String pro_choiceF,String pro_choiceT){
        LinkedHashMap<String,String> map=new LinkedHashMap<>();
        map.put("T",pro_choiceF);
        map.put("E",pro_choiceT);
        xsRequset.setChoiceList(map);
        problem.setPro_choice(xsTreamServices.objectToXml(xsRequset));
        java.sql.Date date=new java.sql.Date(new Date().getTime());
        problem.setDate(date);
        return problemServices.updateProblem(problem);
    }*/
    /**
     * 删除试题
     * @return
     */
    @RequestMapping("/deleteProblem")
    @ResponseBody
    public int deleteProblem(Problem problem){

        return problemServices.deleteProblem(problem);
    }

}
